package com.hz.ws;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hz.base.beans.vo.CommonResult;
import com.hz.base.beans.vo.PageEntity;
import com.hz.base.beans.vo.QueryParams;
import com.hz.beans.user.entity.BaseUser;
import com.hz.beans.user.entity.PRole;
import com.hz.interfaces.user.inf.IBaseUserInterface;
import com.hz.interfaces.user.inf.IPRoleInterface;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Api(tags = "站点角色管理",description = "operation about pRole")
@RestController
@RequestMapping("/pRole")
public class Test1Controller {
	@Autowired
	private IPRoleInterface pRoleInterface;
	@Autowired
	private IBaseUserInterface baseUserInterface;
	
	@ApiOperation(value="获取角色信息", notes="根据url的id来获取角色详细信息")
	@ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long", paramType = "query")
	@RequestMapping(value = "/query" ,method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult<PRole> testQuery(@RequestParam Long id) {
		return pRoleInterface.queryPRole(id);
	}
	
	@ApiOperation(value="获取角色page", notes="根据url的queryParams来获取角色列表详细信息")
	@RequestMapping(value = "/queryForPage" ,method = {RequestMethod.POST,RequestMethod.GET})
	public CommonResult<PageEntity> queryForPage(@RequestBody QueryParams<PRole> queryParams) {
		return pRoleInterface.queryForPage(queryParams);
	}
	
	@ApiOperation(value="获取角色page", notes="根据url的queryParams来获取角色列表详细信息")
	@RequestMapping(value = "/queryForUserPage" ,method = {RequestMethod.POST,RequestMethod.GET})
	public CommonResult<PageEntity> queryForUserPage(@RequestBody QueryParams<BaseUser> queryParams) {
		return baseUserInterface.queryForPage(queryParams);
	}
	
	@ApiOperation(value="添加角色", notes="根据post参数信息添加角色")
	@RequestMapping(value = "/add" ,method = RequestMethod.POST)
	public CommonResult<PRole> add(@RequestBody PRole pRole) {
		return pRoleInterface.addPRole(pRole);
	}
	
	@RequestMapping(value = "/updatePRole" ,method = RequestMethod.POST)
	public CommonResult<PRole> updatePRole(PRole pRole){
		return pRoleInterface.updatePRole(pRole);
	}

	@RequestMapping(value = "/deletePRoleArray" ,method = RequestMethod.POST)
	public CommonResult<Object> deletePRoleArray(Long[] ids){
		return pRoleInterface.deletePRoleArray(ids);
	}

	@RequestMapping(value = "/insertSelective" ,method = RequestMethod.POST)
	public CommonResult<PRole> insertSelective(PRole pRole){
		return pRoleInterface.insertSelective(pRole);
	}
	
	@RequestMapping(value = "/deleteByList" ,method = RequestMethod.POST)
	public CommonResult<Object> deleteByList(@RequestBody List<PRole> pRoleList){
		return pRoleInterface.deleteByList(pRoleList);
	}
	
	@RequestMapping(value = "/batchUpdate" ,method = RequestMethod.POST)
	public CommonResult<List<PRole>> batchUpdate(@RequestBody String listStr){
		ObjectMapper objectMapper = new ObjectMapper();
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, PRole.class);
		List<PRole> pRoleList = null;
		try {
			pRoleList = objectMapper.readValue(listStr, javaType);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pRoleInterface.batchUpdate(pRoleList);
	}
	
	@RequestMapping(value = "/batchSave" ,method = RequestMethod.POST)
	public CommonResult<List<PRole>> batchSave(@RequestBody String listStr){
		ObjectMapper objectMapper = new ObjectMapper();
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, PRole.class);
		List<PRole> pRoleList = null;
		try {
			pRoleList = objectMapper.readValue(listStr, javaType);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pRoleInterface.batchSave(pRoleList);
	}
	
}

